#include "Flag2Control.h"
#include <QDebug>
/**********************************************
* @file       flag2control.cpp
* @模板
{
  "moveHinge": {
    "flag2": {
      "label1": "moveHinge",
      "lineedit": "moveHinge",
      "label2": "xx",
      "pushbutton1": {
        "name": "moveHinge",
        "Commands": "moveHinge(x=<value>)"
      },
      "pushbutton2": {
        "name": "stopHinge",
        "Commands": "stopShuttle()"
      }
    }
  }
}
* @author     yfk
* @date       2025-05-27
**********************************************/

Flag2Control::Flag2Control(const QJsonObject &flagDetails,const QString deviceID, QWidget *parent)
    : QWidget(parent)
{
    setupControlLayout(flagDetails);  // 设置控件的布局
    mDeviceName = deviceID;
}

void Flag2Control::setupControlLayout(const QJsonObject &flagDetails)
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // ========== 外部容器居中 ==========
    QWidget *outerWidget = new QWidget(this);
    QHBoxLayout *outerLayout = new QHBoxLayout(outerWidget);
    outerLayout->setContentsMargins(0, 0, 0, 0);

    // ========== 内容控件区域 ==========
    QWidget *contentWidget = new QWidget(outerWidget);
    contentWidget->setStyleSheet(R"(
        QWidget {
            background-color: #22314a;
            border-radius: 6px;
            padding: 8px 14px;
        }
    )");
    QHBoxLayout *contentLayout = new QHBoxLayout(contentWidget);
    contentLayout->setContentsMargins(8, 2, 8, 2);
    contentLayout->setSpacing(16);

    // 标签
    QLabel *label = new QLabel(flagDetails["label1"].toString(), contentWidget);
    label->setStyleSheet(R"(
        QLabel {
            font-size: 18px;
            font-weight: bold;
            color: white;
        }
    )");
    contentLayout->addWidget(label);

    // 输入框
    lineEdit = new QLineEdit(contentWidget);
    lineEdit->setObjectName(flagDetails["lineedit"].toString());
    lineEdit->setFixedWidth(150);
    lineEdit->setStyleSheet(R"(
        QLineEdit {
            background-color: #374151;
            border-radius: 4px;
            padding: 6px 10px;
            color: white;
            font-size: 16px;
            border: none;
        }
    )");
    contentLayout->addWidget(lineEdit);

    // 记录
    lineEditMap.insert(flagDetails["lineedit"].toString(), lineEdit);

    // ========== 解析按钮信息 ==========
    QJsonObject pushButton1 = flagDetails["pushbutton1"].toObject();
    QJsonObject pushButton2 = flagDetails["pushbutton2"].toObject();

    QString text1 = pushButton1["name"].toString();
    QString text2 = pushButton2["name"].toString();

    buttonCommandMap.insert(text1, pushButton1["Commands"].toString());
    buttonCommandMap.insert(text2, pushButton2["Commands"].toString());

    // ========== 第一个按钮（蓝色）==========
    QPushButton *button1 = new QPushButton(QIcon(":/img/start.png"), text1, contentWidget);
    button1->setObjectName("btn_" + text1);
    button1->setCursor(Qt::PointingHandCursor);
    button1->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #2196F3, stop:1 #0D47A1);
            color: white;
            font-size: 16px;
            font-weight: bold;
            padding: 8px 18px;
            border: none;
            border-radius: 6px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #0D47A1, stop:1 #2196F3);
        }
    )");
    connect(button1, &QPushButton::clicked, this, &Flag2Control::onButtonClicked);
    contentLayout->addWidget(button1);

    // ========== 第二个按钮（橙色）==========
    QPushButton *button2 = new QPushButton(QIcon(":/img/stop.png"), text2, contentWidget);
    button2->setObjectName("btn_" + text2);
    button2->setCursor(Qt::PointingHandCursor);
    button2->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #FFC107, stop:1 #FF6F00);
            color: #333;
            font-size: 16px;
            font-weight: bold;
            padding: 8px 18px;
            border: none;
            border-radius: 6px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #FF6F00, stop:1 #FFC107);
        }
    )");
    connect(button2, &QPushButton::clicked, this, &Flag2Control::onButtonClicked);
    contentLayout->addWidget(button2);

    // 加入内容布局
    outerLayout->addWidget(contentWidget);
    mainLayout->addWidget(outerWidget);
}

void Flag2Control::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (button) {
        QString buttonText = button->text();
        // qDebug() << "Button clicked: " << buttonText;

        // // 查找并执行对应的命令
        // if (buttonCommandMap.contains(buttonText)) {
        //     QString command = buttonCommandMap.value(buttonText);
        //     qDebug() << "Executing command: " << command;

        //     // 获取 QLineEdit 的值
        //     QString lineEditValue = lineEdit->text();
        //     qDebug() << "Line edit value: " << lineEditValue;
        //     command.replace("<value>",lineEditValue);
        //     pubMsg(command);
        //     // 在这里可以执行命令，或者调用其他函数来处理命令和 lineEdit 的值
        // } else {
        //     qWarning() << "No command found for button: " << buttonText;
        // }
    }
}


void Flag2Control::attachToNode(QSharedPointer<DeviceNode> node)
{
    m_node = node;
}

void Flag2Control::pubMsg(QString content)
{
    if(m_node)
    {
        QString id = mDeviceName;
        //QString id = "ORS_Cask";
        m_node->publishCmd(id,content);
    }
}
